﻿using OurMemory.Model.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OurMemory
{
    internal class OurFavorServices
    {
        /// <summary>
        /// 获得所有数据
        /// </summary>
        /// <returns></returns>
        public List<OurFaver> GetAll()
        {
            var db = new OurMemoryEntities();
            return db.OurFaver.ToList();
        }
        /// <summary>
        /// 新建记录
        /// </summary>
        /// <param name="model"></param>
        public void Create(OurFavorCreateModel model)
        {
            var db = new OurMemoryEntities();
            var newItem = new OurFaver()
            {
                Id = Guid.NewGuid(),
                Who = model.Who,
                Wishes = model.Wishes,
                Favor = model.Favor,
                Photo = model.Photo,
            };
            db.OurFaver.Add(newItem);
            db.SaveChanges();
        }
        /// <summary>
        /// 根据Id，获得指定记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OurFaver GetById(Guid id)
        {
            var db = new OurMemoryEntities();
            return db.OurFaver.Find(id);
        }

        /// <summary>
        /// 修改记录
        /// </summary>
        /// <param name="model"></param>
        public void Edit(OurFavorEditModel model)
        {
            var db = new OurMemoryEntities();
            var item = db.OurFaver.Find(model.Id);
            if (item != null)
            {
                item.Who = model.Who;
                item.Wishes = model.Wishes;
                item.Favor = model.Favor;
                item.Photo = model.Photo;
            }
            db.SaveChanges();
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="id"></param>
        public void Delete(Guid id)
        {
            var db = new OurMemoryEntities();
            var item = db.OurFaver.Find(id);
            if (item != null)
            {
                db.OurFaver.Remove(item);
                db.SaveChanges();
            }
        }
    }
}
